package fr.resasante.utils;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import javax.naming.NamingException;

import org.jboss.logging.Logger;

/**
 * Class for getting system properties
 * 
 * @author GEFFROY-DE-VILLEBLAN
 * 
 */
public final class PropertiesUtils {
	/**
	 * Properties class instance
	 */
	private static volatile PropertiesUtils instance = null;

	/**
	 * Logger for the class
	 */
	private static final Logger logger = Logger.getLogger(PropertiesUtils.class.getName());

	/**
	 * Properties
	 */
	private Properties properties = null;

	/**
	 * Constructor
	 */
	private PropertiesUtils() {
		super();
		getAllProperties();
	}

	/**
	 * Method returning a Singleton class instance
	 * 
	 * @return Singleton instance
	 * @throws NamingException
	 */
	public final static PropertiesUtils getInstance() {
		if (PropertiesUtils.instance == null) {
			synchronized (PropertiesUtils.class) {
				if (PropertiesUtils.instance == null) {
					PropertiesUtils.instance = new PropertiesUtils();
				}
			}
		}
		return PropertiesUtils.instance;
	}

	/**
	 * Get a property value from key
	 * 
	 * @param key
	 *            The property key
	 * @return The value of the properties
	 */
	public String getProperty(String key) {
		if (null != properties) {
			return properties.getProperty(key);
		}
		return "";
	}

	/**
	 * Load the properties file
	 */
	private void getAllProperties() {
		InputStream settingsStream = this.getClass().getClassLoader()
				.getResourceAsStream("settings.properties");
		try {
			properties = new Properties();
			properties.load(settingsStream);
		} catch (IOException e) {
			logger.error(CommonUtils.getInstance().getStackTrace(e));
		}
	}
}